package com.ccn.shardingjdbc.controller;

import com.ccn.shardingjdbc.entity.Cars;
import com.ccn.shardingjdbc.entity.User;
import com.ccn.shardingjdbc.entity.Users;
import com.ccn.shardingjdbc.mapper.CarsMapper;
import com.ccn.shardingjdbc.mapper.UserMapper;
import com.ccn.shardingjdbc.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author wangxy
 * @date 2021/7/2 11:07
 */
@RestController
public class TestController {

    @Resource
    private UsersMapper usersMapper;
    @Resource
    private UserMapper userMapper;
    @Autowired
    private CarsMapper carsMapper;
    @GetMapping("/save")
    public String insert() {
        Users user = new Users();
        user.setName("wxy"+ new Random().nextInt());
        usersMapper.addUser(user);
        return "success";
    }
    @GetMapping("/listusers")
    public List<Users> listusers() {
        return usersMapper.findUsers();
    }

    @GetMapping("/saveCars")
    public String inserCars(){
        Cars cars = new Cars();
        cars.setName("小鹏");
        cars.setColor("red");
        cars.setSize("300");
        int i = carsMapper.saveCar(cars);
        if (i>0) {
            return "成功";
        }
        return "失败";
    }
    @GetMapping("/listCars")
    private List<Cars> listCars(){
        List<Cars> cars = carsMapper.listCars();
        return cars;
    }


    @GetMapping("/listuser")
    public List<User> listuser() {
        return userMapper.findUsers();
    }

}
